//
//  SEGOldLoginRightView.m
//  Login
//
//  Created by 梁昆 on 16/8/16.
//  Copyright © 2016年 segiK. All rights reserved.
//
#import "SEGOldLoginRightView.h"
#import <Commom/UITextField+Limit.h>
#import <Commom/SEGSafaKeyboard.h>

@interface SEGOldLoginRightView () <UITextFieldDelegate, SGETextFieldLimitDelegate>
/**
 隐私政策
 */
@property (nonatomic, strong)SEGLabel *privacyLabel;
@end

@implementation SEGOldLoginRightView

- (instancetype)init
{
    if (self == [super init]) {

        self.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.headImageView];
        [self.headImageView mas_makeConstraints:^(MASConstraintMaker* make) {
            make.top.equalTo(self.mas_top).mas_offset(56);
            make.centerX.equalTo(self);
        }];
        
        [self addSubview:self.line1View];
        [self.line1View mas_makeConstraints:^(MASConstraintMaker* make) {
            make.top.equalTo(self.headImageView.mas_bottom).mas_offset(63 * kScaleHeight);
            make.centerX.equalTo(self.mas_centerX);
            make.width.mas_equalTo(255);
            make.height.mas_equalTo(0.5);
        }];
        
        [self addSubview:self.line2View];
        [self.line2View mas_makeConstraints:^(MASConstraintMaker* make) {
            make.top.equalTo(self.line1View).mas_offset(50);
            make.centerX.equalTo(self.mas_centerX);
            make.width.mas_equalTo(255);
            make.height.mas_equalTo(0.5);
        }];
        
        [self addSubview:self.phoneText];
        [self.phoneText mas_makeConstraints:^(MASConstraintMaker* make) {
            make.bottom.equalTo(self.line1View.mas_top);
            make.left.equalTo(self.line1View.mas_left);
            make.width.mas_equalTo(self.line1View).mas_offset(-10);
            make.height.mas_equalTo(13 + 22);
        }];
        
        [self addSubview:self.cleanPhoneButton];
        [self.cleanPhoneButton mas_makeConstraints:^(MASConstraintMaker* make) {
            make.bottom.equalTo(self.line1View.mas_top).mas_offset(-6);
            make.right.equalTo(self.line1View.mas_right);
            make.width.mas_equalTo(24);
            make.height.mas_equalTo(24);
        }];
        
        [self addSubview:self.passwordText];
        [self.passwordText mas_makeConstraints:^(MASConstraintMaker* make) {
            make.bottom.equalTo(self.line2View.mas_top);
            make.left.equalTo(self.line2View.mas_left);
            make.width.mas_equalTo(self.line2View);
            make.height.mas_equalTo(13 + 22);
        }];
        
        [self addSubview:self.losepassButton];
        [self.losepassButton mas_makeConstraints:^(MASConstraintMaker* make) {
            make.bottom.equalTo(self.line2View.mas_top);
            make.right.equalTo(self.line2View.mas_right);
            make.height.mas_equalTo(40);
        }];
        
        [self addSubview:self.enterButton];
        [self.enterButton mas_makeConstraints:^(MASConstraintMaker* make) {
            make.top.equalTo(self.line2View.mas_bottom).mas_offset(55);
            make.centerX.equalTo(self.mas_centerX);
            make.width.mas_equalTo(260);
            make.height.mas_equalTo(40);
        }];
        
        //计算按钮文本的宽高
        CGSize size = [self.gotoRegister.titleLabel.text boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} context:nil].size;
        
        [self addSubview:self.gotoRegister];
        [self.gotoRegister mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.enterButton.mas_bottom).mas_offset(20 * kScaleHeight);
            make.height.mas_equalTo(size.height);
            make.centerX.equalTo(self.mas_centerX);
        }];
        
        //隐私政策
        [self addSubview:self.privacyLabel];
        [self.privacyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.bottom.equalTo(self).offset(-30-LYSafeAreaBottom());
        }];
        
    }
    return self;
}

//设置手机号和密码
- (void)setPhoneText:(NSString *)phone passwordText:(NSString *)password
{
    self.phoneText.text = phone;
    self.passwordText.text = password;
    if (phone.length > 0 && password.length > 0) {
        self.enterButton.enabled = YES;
    }else {
        self.enterButton.enabled = NO;
    }
}

#pragma mark - LoginDelegate
// 提示用户去注册
-(void)letGotoRegister{
    [self.loginDelegate GotoRegister];
}

//  忘记密码
- (void)losepassclick
{
    [self.loginDelegate losePasswordWithPhoneNumber:self.phoneText.text];
}

//获取进入app 网络请求
- (void)enterButtonClick
{
    [self endEditing:YES];

    if (self.phoneText.text.length != 11) {
        [SEGAlertView showTmepMessageWithMessage:@"请输入正确的手机号码"];
    }
    else {
        NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:self.phoneText.text, @"tel", self.passwordText.text, @"password", nil];
        [self.loginDelegate enterLogin:dict];
    }
}

#pragma mark - 方法实现
//清空手机号
- (void)cleanPhoneText
{
    self.phoneText.text = nil;
    self.cleanPhoneButton.hidden = YES;
    self.enterButton.enabled = NO;
}

- (void)textFieldDidBeginEditing:(UITextField*)textField
{
    //控制清空按钮的显示
    if ([textField isEqual:self.phoneText]) {
        if (textField.text.length > 0) {
            self.cleanPhoneButton.hidden = NO;
        }
        else {
            self.cleanPhoneButton.hidden = YES;
        }
    }else if ([textField isEqual:self.passwordText]) {
    }
}

- (BOOL)textFieldShouldReturn:(UITextField*)textField
{
    if (textField == self.passwordText && textField.text.length > 0) {
        [self enterButtonClick];
    }
    return YES;
}

#pragma mark - SGETextFieldLimitDelegate
- (void)limitTextFileDidChange:(UITextField *)textField {
    if (self.phoneText.text.length > 0 && self.passwordText.text.length > 0) {
      self.enterButton.enabled = YES;
    } else {
      self.enterButton.enabled = NO;
    }
    self.cleanPhoneButton.hidden = self.phoneText.text.length > 0 ? NO:YES;
}

#pragma mark -  getter  控件视图
//顶部图片
- (UIImageView*)headImageView {
    if (!_headImageView) {
        _headImageView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:SEGPicName(@"img_login")]];
    }
    return _headImageView;
}

//第一根灰色分割线
- (UIView*)line1View
{
    if (!_line1View) {
        _line1View = [[UIView alloc] init];
        _line1View.backgroundColor = kColor_L2;
    }
    return _line1View;
}

- (UIView*)line2View
{
    if (!_line2View) {
        _line2View = [[UIView alloc] init];
        _line2View.backgroundColor = kColor_L2;
    }
    return _line2View;
}

//输入手机号码textfield
- (UITextField*)phoneText
{
    if (!_phoneText) {
        _phoneText = [[UITextField alloc] init];
        //        _phoneText.backgroundColor = [UIColor redColor];
        [_phoneText setTextColor:kColor_1];
        [_phoneText setFont:[UIFont systemFontOfSize:13]];
        [_phoneText setAttributedPlaceholder:[[NSAttributedString alloc] initWithString:@"手机号" attributes:@{NSForegroundColorAttributeName:kColor_C}]];
        _phoneText.clearsOnBeginEditing = NO; //再次编辑不清空
        _phoneText.clearButtonMode = UITextFieldViewModeNever;
        _phoneText.delegate = self;
        _phoneText.keyboardType = UIKeyboardTypeNumberPad;
        //        [_phoneText resignFirstResponder];
        [_phoneText setTintColor:kColor_T];
        [_phoneText configPhoneInputLimitWithDelegate:self];
    }
    return _phoneText;
}

//输入密码
- (SEGPasswordTextField*)passwordText
{
    if (!_passwordText) {
        _passwordText = [[SEGPasswordTextField alloc] init];
        [_passwordText setTextColor:kColor_1];
        [_passwordText setTintColor:kColor_T];
        [_passwordText setFont:[UIFont systemFontOfSize:13]];
        [_passwordText setAttributedPlaceholder:[[NSAttributedString alloc] initWithString:@"密码" attributes:@{NSForegroundColorAttributeName:kColor_C}]];
        _passwordText.secureTextEntry = YES;
        _passwordText.clearsOnBeginEditing = NO; //再次编辑不清空
        _passwordText.delegate = self;
        [_passwordText configPasswordInputLimitWithDelegate:self currentView:self];
    }
    return _passwordText;
}

//清空电话按钮
- (UIButton*)cleanPhoneButton
{
    if (!_cleanPhoneButton) {
        //发送验证码 按钮
        _cleanPhoneButton = [[UIButton alloc] init];
        [_cleanPhoneButton setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"icon_list_delete")] forState:UIControlStateNormal];
        [_cleanPhoneButton addTarget:self action:@selector(cleanPhoneText) forControlEvents:UIControlEventTouchUpInside];
        _cleanPhoneButton.hidden = YES;
    }
    return _cleanPhoneButton;
}

//“忘记密码”点击按钮
- (UIButton*)losepassButton
{
    if (!_losepassButton) {
        _losepassButton = [[UIButton alloc] init];
        [_losepassButton setTitle:@"忘记密码" forState:UIControlStateNormal];
        _losepassButton.titleLabel.font = [UIFont systemFontOfSize:13];
        [_losepassButton setTitleColor:kColor_2 forState:UIControlStateNormal];
        [_losepassButton addTarget:self action:@selector(losepassclick) forControlEvents:UIControlEventTouchUpInside];
        _losepassButton.backgroundColor = [UIColor whiteColor];
    }
    return _losepassButton;
}

//“进入app”点击按钮
- (UIButton*)enterButton
{
    if (!_enterButton) {
        _enterButton = [[UIButton alloc] init];
        _enterButton.backgroundColor = kColor_T;
        [_enterButton setBackgroundImage:[UIImage imageWithColor:kColor_T] forState:UIControlStateNormal];
        [_enterButton setBackgroundImage:[UIImage imageWithColor:kColor_T_Press] forState:UIControlStateHighlighted];
        [_enterButton setBackgroundImage:[UIImage imageWithColor:kColor_L2] forState:UIControlStateDisabled];
        [_enterButton setTitle:@"登录" forState:UIControlStateNormal];
        _enterButton.titleLabel.font = [UIFont systemFontOfSize:16];
        [_enterButton setTitleColor:kColor_W forState:UIControlStateNormal];
        _enterButton.layer.masksToBounds = YES;
        _enterButton.layer.cornerRadius = 20;
        [_enterButton addTarget:self action:@selector(enterButtonClick) forControlEvents:UIControlEventTouchUpInside];
        _enterButton.enabled = NO;
    }
    return _enterButton;
}

// 提示用户注册
- (UIButton*)gotoRegister
{
    if (!_gotoRegister) {
        _gotoRegister = [[UIButton alloc] init];
        [_gotoRegister setTitle:@"还没有账号，去注册" forState:UIControlStateNormal];
        [_gotoRegister setTitleColor:kColor_T forState:UIControlStateNormal];
        _gotoRegister.titleLabel.font = [UIFont systemFontOfSize:13];
        _gotoRegister.tag = 1;
        [_gotoRegister addTarget:self action:@selector(letGotoRegister) forControlEvents:UIControlEventTouchUpInside];
    }
    return _gotoRegister;
}

//隐私政策
- (SEGLabel *)privacyLabel
{
    if (!_privacyLabel) {
        _privacyLabel = [[SEGLabel alloc] init];
        _privacyLabel.textAlignment = NSTextAlignmentCenter;
        NSString *str1 = @"登录即代表您已经同意";
        NSString *str2 = [NSString stringWithFormat:@"%@用户协议", kAppName];
        NSString *str3 = [NSString stringWithFormat:@"和隐私政策"];
        NSString *str = [NSString stringWithFormat:@"%@%@%@",str1,str2,str3];

        NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:str];
        text.yy_font = kFont_11;
        text.yy_color = kColor_3;
        //        text.yy_lineSpacing = 5;
        
        __weak typeof(self) weakSelf = self;
        //点击
        NSRange range = [str rangeOfString:@"隐私政策"];
        [text yy_setTextHighlightRange:range
                                 color:kColor_1
                       backgroundColor:[UIColor clearColor]
                             tapAction:^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect){
                                 if (weakSelf.loginDelegate && [weakSelf.loginDelegate respondsToSelector:@selector(gotoPrivacy)]) {
                                     [weakSelf.loginDelegate gotoPrivacy];
                                 }
                             }];
        NSRange range2 = [str rangeOfString:@"用户协议"];
        [text yy_setTextHighlightRange:range2
                                 color:kColor_1
                       backgroundColor:[UIColor clearColor]
                             tapAction:^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect){
                                 if (weakSelf.loginDelegate && [weakSelf.loginDelegate respondsToSelector:@selector(gotoUserAgreement)]) {
                                     [weakSelf.loginDelegate gotoUserAgreement];
                                 }
                             }];
        [text yy_setUnderlineStyle:NSUnderlineStyleSingle range:range];
        [text yy_setUnderlineStyle:NSUnderlineStyleSingle range:range2];

        _privacyLabel.attributedText = text;
    }
    return _privacyLabel;
}
@end
